import string
import sys
import process_ebk as ebk
import process_mail as mail
import process_html as html
import string_operations as str_op
import file_operations as file_op
from document import *


class indexer:
    
    def __init__(self):
        self.doclist=[]
        return
    
    def storeIndex(self,indexloc):
        """
        We pickle the index location and 
        store it in doclist
        """
        (file_name, file_type) = self.identifyType(indexloc)
        if (file_type == 'index'):
            file_op.storeIndex(self.doclist, indexloc)
        else:
            file_op.storeIndex(self.doclist, indexloc + '.index')
        
    def openIndex(self,indexloc):
        """
        Get the content of the file in indexloc and save it
        into a list
        """
        (file_name, file_type) = self.identifyType(indexloc)
        if(file_type == 'index'):
            self.doclist = file_op.openIndex(indexloc)
        else:
            self.myModel.dialog('Not a Valid Index.')
        
    def removefromIndex(self,input_file):
        """
        Remove the document from the index
        """
        new_doc = self.initializeDoc(input_file)
        self.removeDoc(new_doc)
    
    def initializeDoc(self, input_file):
        """
        Initialize the document based on input_file's name and type
        """
        (file_name, file_type) = self.identifyType(input_file)
        (file_name, file_loc) = self.identifyLoc(file_name)
        return document(file_loc, file_name, '.'+file_type)
    
    def identifyLoc(self, file_name):
        """
        Return the extension and the file name
        """
        temp_list = file_name.rpartition('\\')
        return temp_list[2], temp_list[0]+temp_list[1]       
    
    def removeDoc(self, new_doc):
        """
        Removes the document form the doclist if there already
        exists another document of the same filename and same filetype
        """
        if new_doc in self.doclist:
            self.doclist.remove(new_doc)

    def clearInder(self):
        """
        Clear the doc_list
        """
        self.doclist = []        
    
    def addtoIndex(self,input_file):
        """
        Main method, add a document into index
        """
        new_doc = self.initializeDoc(input_file)
        (file_name, file_type) = self.identifyType(input_file)
        input_string = file_op.read(input_file)
        if input_string== '':
            return False
        stem_list = self.parseInput(input_string, file_type, new_doc)
        new_doc.indexDoc(stem_list)
        self.addDoc(new_doc)
        return True
  
    def addDoc(self,new_doc):
        """
        Add a document to doclist
        """
        if new_doc in self.doclist:
            self.doclist.remove(new_doc)
            self.dialog('File was already in the index. The old indexing has been replaced.')
        self.doclist.append(new_doc) 

    def parseInput(self,input_string, file_type, new_doc):
        """
        Use string operations to parse the text
        """
        method = getattr(self, 'parse'+file_type, None)
        if callable(method):
            return method(input_string, new_doc)
        else:
            return self.parsetxt(input_string, new_doc)

    def parsetxt(self, input_string, new_doc):
        """
        Parse the text
        """
        return str_op.parseString(input_string)

    def parsehtml(self, input_string, new_doc):
        """
        Parse the html content
        """
        return html.parseHTML(input_string, new_doc)
    
    def parsemail(self, input_string, new_doc):
        """
        Parse the mail content
        """
        return mail.parseMail(input_string, new_doc)

    def parseebk(self, input_string, new_doc):
        """
        Parse the ebook content
        """
        return ebk.parseEbk(input_string, new_doc)

    def identifyType(self,input_file):
        """
        Input: file_input is a string
        ----------------------
        Output:
        First turn the file name into a 3-element list 
        which is separated by the last occurrence
        of a period. 
    
        Then return the file_name (which is the first element), 
        and the file_type (the last element)
           """
        file_data = input_file.rpartition('.')
        return file_data[0], file_data[2]
    
    def getDocNames(self):
        docnames = []
        for doc in self.doclist:
            docnames.append(doc.getLocation())
        return docnames
    